﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

using SpectrEngine.Framework;
using SpectrEngine.Framework.Input;

namespace CellWarzGame.Screens
{
    class MainMenuScreen
        : GameScreen
    {
        const int MaxVolume = 8;

        Texture2D backdrop;
        Texture2D mainMenuCell;
        Texture2D title;
        Texture2D[] hilites;

        Texture2D volumeCell;
        Texture2D volumeOn;
        Texture2D volumeOff;

        int volumeLevel = 5;

        double cooldown = 0.0f;

        int selectedItem = 0;

        TimeSpan selectCooldown = TimeSpan.FromSeconds(0.2);
        double selectTimer = 0.0;

        bool changeSound = false;

        SoundEffect selectionSound;

        public MainMenuScreen(Game game)
            : base(game)
        {

            this.State = ScreenState.Active;

            //this.transitionMask = game.Content.Load<Texture2D>("Test\\organic_fade");

            game.Services.GetService<ScreenManager>().AddScreen(this, 0);

            backdrop = game.Content.Load<Texture2D>(@"Background\CWZ_MENU_BG");
            mainMenuCell = game.Content.Load<Texture2D>(@"Menu\GUI_MAIN_MENU_CELL");
            title = game.Content.Load<Texture2D>(@"Menu\GUI_TITLE");

            hilites = new Texture2D[4];
            hilites[0] = game.Content.Load<Texture2D>(@"Menu\GUI_HILITE_1");
            hilites[1] = game.Content.Load<Texture2D>(@"Menu\GUI_HILITE_2");
            hilites[2] = game.Content.Load<Texture2D>(@"Menu\GUI_HILITE_3");
            hilites[3] = game.Content.Load<Texture2D>(@"Menu\GUI_HILITE_4");

            volumeCell = game.Content.Load<Texture2D>(@"Menu\GUI_VOLUME_CELL");
            volumeOn = game.Content.Load<Texture2D>(@"Menu\VOLUME_CELL_ON");
            volumeOff = game.Content.Load<Texture2D>(@"Menu\VOLUME_CELL_OFF");

            selectionSound = game.Content.Load<SoundEffect>("Sounds\\pop");
            SoundEffect.MasterVolume = (float)volumeLevel / (float)MaxVolume;
        }

        public override void Update(GameTime gameTime)
        {

            if (cooldown < 0.5)
            {
                cooldown += gameTime.ElapsedGameTime.TotalSeconds;
            }
            else if (changeSound)
            {
                if (GamePadManager.IsButtonPressed(PlayerIndex.One, GamePadButtons.A)
                            || GamePadManager.IsButtonPressed(PlayerIndex.One, GamePadButtons.B))
                {
                    changeSound = false;
                    selectTimer = selectCooldown.TotalSeconds * 2;
                }
                else if (selectTimer < 0.1)
                {
                    Vector2 thumbDir = GamePadManager.GetThumbstick(PlayerIndex.One, GamePadThumbSticks.Left);

                    if (thumbDir.Y > 0.1)
                    {
                        if (volumeLevel < MaxVolume)
                        {
                            selectionSound.Play();
                            volumeLevel++;
                            selectTimer = 0.3;
                            SoundEffect.MasterVolume = (float)volumeLevel / (float)MaxVolume;
                        }
                    }
                    else if (thumbDir.Y < -0.1)
                    {
                        if (volumeLevel > 0)
                        {
                            selectionSound.Play();
                            volumeLevel--;
                            selectTimer = 0.3;
                            SoundEffect.MasterVolume = (float)volumeLevel / (float)MaxVolume;
                        }
                    }
                }
               
            }
            else if(selectTimer < 0.1)
            {
                selectTimer = selectCooldown.TotalSeconds * 1.5;

                Vector2 thumbDir =  GamePadManager.GetThumbstick(PlayerIndex.One, GamePadThumbSticks.Left);
                if (thumbDir.Y > 0.1)
                {
                    selectedItem--;
                    if (selectedItem < 0)
                    {
                        selectedItem = 3;
                    }
                    selectionSound.Play();
                }
                else if (thumbDir.Y < -0.1)
                {
                    selectedItem++;
                    if (selectedItem > 3)
                    {
                        selectedItem = 0;
                    }
                    selectionSound.Play();
                }
                else
                {
  
                    switch (selectedItem)
                    {
                        case 0:
                            if (GamePadManager.IsButtonDown(PlayerIndex.One, GamePadButtons.A)
                                || GamePadManager.IsButtonDown(PlayerIndex.One, GamePadButtons.B))
                            {
                                new GamePlayScreen(Game);
                                Game.Services.GetService<ScreenManager>().RemoveScreen(this);
                                selectionSound.Play();
                            }
                            break;
                        case 1:
                            if (GamePadManager.IsButtonDown(PlayerIndex.One, GamePadButtons.A)
                                || GamePadManager.IsButtonDown(PlayerIndex.One, GamePadButtons.B))
                            {
                                selectionSound.Play();
                                changeSound = true;
                            }
                            break;
                        case 2:
                            break;
                        case 3:
                            if (GamePadManager.IsButtonDown(PlayerIndex.One, GamePadButtons.A)
                                || GamePadManager.IsButtonDown(PlayerIndex.One, GamePadButtons.B))
                            {
                                selectionSound.Play(); 
                                Game.Exit();
                            }
                            break;
                    }
                    
                    
                }
            }

            if (selectTimer > 0.0)
            {
                selectTimer = Math.Max(selectTimer - gameTime.ElapsedGameTime.TotalSeconds, 0.0) ;
            }

            base.Update(gameTime);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            Game.GraphicsDevice.Clear(Color.White);

            //spriteBatch.Draw(backdrop, new Vector2(this.Game.GraphicsDevice.Viewport.Width / 2, this.Game.GraphicsDevice.Viewport.Height / 2), null, Color.White, 0.0f, new Vector2(backdrop.Width / 2, backdrop.Height / 2), 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.Draw(backdrop, Vector2.Zero, Color.White);
            spriteBatch.Draw(mainMenuCell, Vector2.Zero, Color.White);
            spriteBatch.Draw(title, Vector2.Zero, Color.White);

            spriteBatch.Draw(hilites[selectedItem], Vector2.Zero, Color.White);

            if (changeSound)
            {
                spriteBatch.Draw(volumeCell, Vector2.Zero, Color.White);
                spriteBatch.Draw(volumeOn, new Vector2(215.0f, 205.0f), getVolumeColor(8));
                spriteBatch.Draw(volumeOn, new Vector2(215.0f, 230.0f), getVolumeColor(7));
                spriteBatch.Draw(volumeOn, new Vector2(215.0f, 255.0f), getVolumeColor(6));
                spriteBatch.Draw(volumeOn, new Vector2(215.0f, 280.0f), getVolumeColor(5));
                spriteBatch.Draw(volumeOn, new Vector2(215.0f, 305.0f), getVolumeColor(4));
                spriteBatch.Draw(volumeOn, new Vector2(215.0f, 330.0f), getVolumeColor(3));
                spriteBatch.Draw(volumeOn, new Vector2(215.0f, 355.0f), getVolumeColor(2));
                spriteBatch.Draw(volumeOn, new Vector2(215.0f, 380.0f), getVolumeColor(1));
            }
            

            base.Draw(gameTime);
        }

        private Color getVolumeColor(int level)
        {
            if (level <= volumeLevel)
            {
                return Color.White;
            }

            return Color.Black;
        }
    }
}
